package coco.core
{
	import flash.events.TimerEvent;
	import flash.utils.Timer;
	
	import spark.components.WindowedApplication;
	
	public class AutoHide
	{
		public static const LEFT_SIDE:String = 'leftSide';
		public static const RIGHT_SIDE:String = 'rightSide';
		public static const TOP_SIDE:String = 'topSide';
		public var valueFrom:Number = 0;
		public var valueTo:Number = 0;
		public var duation:Number = 150; 
		public var target:WindowedApplication;
		public var direction:String = LEFT_SIDE;
		
		private var timer:Timer;
		private var value:Number;
		private var gap:Number;
		public function play():void
		{
			trace('play ' + direction);
			switch(direction)
			{
				case AutoHide.LEFT_SIDE:
					valueFrom = target.nativeWindow.x;
					break;
				case AutoHide.RIGHT_SIDE:
					valueFrom = target.nativeWindow.x;
					break;
				case AutoHide.TOP_SIDE:
					valueFrom = target.nativeWindow.y;
					break;
			}
			
			if(timer && timer.running)
			{
				timer.stop();
				timer.removeEventListener(TimerEvent.TIMER, onTimer);
			}
			
			var repeat:int = duation / 10;
			gap = (valueTo - valueFrom) / repeat;
			value = valueFrom;
			
			timer = new Timer(10, repeat);
			timer.addEventListener(TimerEvent.TIMER, onTimer);
			timer.start();
		}
		
		private function onTimer(e:TimerEvent):void
		{
			value += gap;
			if(direction == LEFT_SIDE || direction == RIGHT_SIDE)
				moveTo(value, target.nativeWindow.y);
			if(direction == TOP_SIDE)
				moveTo(target.nativeWindow.x, value);
			
			if(timer.currentCount == timer.repeatCount)
			{
				timer.stop();
				timer = null;
			}
		}
		
		
		private function moveTo(x:Number, y:Number):void
		{
			target.move(x, y);
		}
	}
}